#include "IV22.h"
#include "StarEntry.h"
#include <qtextstream.h>
#include <qstringlist.h>
#include <libnova.h>

/*
  Bytes Format Units   Label     Explanations

   1-  4  I4    ---     FK5       The FK5 number (Cat. I/149, I/175) (2)
   7-  8  I2    h       RAh       Right Ascension hours (J2000)
  10- 11  I2    min     RAm       Right Ascension minutes (J2000)
  13- 18  F6.3  s       RAs       Right Ascension seconds (J2000)
  20- 26  F7.3  10ms/yr pmRA      Proper motion in right ascension
      28  A1    ---     DE-       Declination sign
  29- 30  I2    deg     DEd       Declination degrees (J2000)
  32- 33  I2    arcmin  DEm       Declination minutes (J2000)
  35- 39  F5.2  arcsec  DEs       Declination seconds (J2000)
  41- 47  F7.2 10mas/yr pmDE      Proper motion in declination
  49- 53  F5.2  yr     EpRA-1900  Epoch for right ascension -1900
  55- 59  F5.2  yr     EpDE-1900  Epoch for declination -1900
  60- 64  F5.2  mag     Vmag      Visual magnitude
  66- 67  A2    ---     Sp        Spectral type
  69- 78  A10   ---     DM        Durchmusterung Identification (1)
  80- 85  I6    ---     SAO       ? SAO number (Cat. I/131)
  87- 92  I6    ---     HD        ? HD number (Cat. III/135)
  94-102  A9    ---     BFno      Bayer or Flamsteed number
 104-123  A20   ---     name      Common name or variable name
 */

void Iv22::loadData()
{
  QString record;
  int maxrecordlength=123;
  int count=0;
  int fk5;
  int rah, ram;
  float ras, pmRA;
  int ded, dem;
  float des, pmDE;
  float epRA, epDE;
  float mag;
  char sp[2];
  char dm[10];
  QString sao, hd;
  QString bfno;
  QString sname;
  double ra, dec;
  
  QString *uname;
  
  if (!datafile.open(QIODevice::ReadOnly | QIODevice::Text))
    return;

  prefix = new QString("FK5");
  
  QTextStream in(&datafile);
  record[123]='\0';
 
  while (!in.atEnd()) {
    record=in.readLine(maxrecordlength);
    //printf("Record%d\n%s\n", count, record.toAscii().data());
    fk5=record.mid(0,4).toInt();
    rah=record.mid(6,3).toInt();
    ram=record.mid(9,2).toInt();
    ras=record.mid(12,6).toFloat();
    ra=rah +(ram / 60.0) + (ras / 3600.0);
    pmRA=(record.mid(19,7).toFloat() * 15.0);
    ded=record.mid(27,3).toInt();
    dem=record.mid(31,2).toInt();
    des=record.mid(34,6).toFloat();
    if (ded >= 0)
      dec=ded +(dem / 60.0) + (des / 3600.0);
    else
      dec=ded -(dem / 60.0) - (des / 3600.0);
    pmDE=record.mid(40,7).toFloat();
    mag=record.mid(59,5).toFloat();
    sao=record.mid(79,6);
    bfno=QString(); sname=QString();
    if (record.length() > 94) bfno=record.mid(93, 9);
    if (record.length() > 104) sname=record.mid(103);
    //printf("  Parsed IV22 star SAO%s RA=%g DEC=%g mag=%g bfno=%s name=%s\n", sao.toAscii().data(), ra, dec, mag, bfno.toAscii().data(), sname.toAscii().data());
    if (sname.isNull() or sname.isEmpty())
      if (bfno.isNull() or bfno.isEmpty())
	uname=new QString("SAO"+sao);
      else
	uname=new QString(bfno);
    else
      uname=new QString(sname);
    //printf("Star %d: %d -- %g -- %g -- %g -- %s\n", count, fk5, ra, dec, mag, uname->toAscii().data());
    stars.insert(stars.end(), new StarEntry(count, fk5, JD2000, ra, dec, (pmRA / 360000.0), (pmDE / 360000.0), mag, *uname));
    count+=1;
  }
  emit layoutChanged();
  datafile.close();
}